<?php
/**
 * Created by PhpStorm. 物流新API板块
 * User: Administrator
 * Date: 2020/1/4/004
 * Time: 11:15
 */
namespace app\service;

use think\Env;

class DeliverService
{
    /**
     * 京东 & 顺丰 物流API需新增，CustomerName校验
     * @param string $express_code
     * @param $shipping_code
     * @return array
     */
    public function new_get_express($express_code = 'JD',$shipping_code){
        $args = [
            'ShipperCode'   => $express_code,  #目前管易仅支持，邮政国内标快
            'LogisticCode'  => $shipping_code,
            'CustomerName'  => config('jd_customer_name')   #京东物流需要使用CustomerName
        ];

        $apiKey     = Env::get('express.api_key');
        $jsonStr    = json_encode($args,true);
        $RequestData = urlencode($jsonStr);
        $EBusinessID = Env::get('express.business_id');
        #目前使用 及时查询API
        $RequestType = Env::get('express.intime_query_type');
        $DataSign = urlencode(base64_encode(md5($jsonStr.$apiKey)));

        $requestData = [
            'RequestData' => $RequestData,
            'EBusinessID' => $EBusinessID,
            'RequestType' => $RequestType,
            'DataSign'    => $DataSign
        ];
        $res = http_post(Env::get('express.request_url'),$requestData);
        $resArr = json_decode($res,true);

        $output = array();
        if (is_array($resArr['Traces'])) {
            $resArr['Traces'] = array_reverse($resArr['Traces']);
            foreach ($resArr['Traces'] as $k => $v) {
                if ($v['AcceptTime'] == '')   continue;
                $output[$k]['time']     = $v['AcceptTime'];
                $output[$k]['context']  = $v['AcceptStation'];
            }
        }else{
            $output[0]['time']     = TIMESTAMP;
            $output[0]['context']  = "如若查不到物流信息！客官可以前往，<a href='http://www.jdwl.com'>物流官方网站</a>，查询信息喔！";
        }
        //if (empty($output))     ds_json_encode(10001,'暂未获取到该货运单的物流信息！');

        return $output;
    }
}